//
//  ApplyDetailModel.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/28.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import ObjectMapper
import RxSwift

public enum ApplyPersonInfoType: Int {
    case base = 0
    case enter
    case choose
}

class ApplyPersonInfo: Mappable {
    required init?(map: Map) {
        
    }
    
    func mapping(map: Map) {
        titleName <- map["titleName"]
        type <- map["type"]
        placeholder <- map["placeholder"]
        detailText <- map["detailText"]
        save_detailText <- map["save_detailText"]
        
        chooseArray <- map["chooseArray"]
        chooseType <- map["chooseType"]
        chooseModel <- map["chooseModel"]
        
    }
    
    
    var titleName = ""
    var type = ApplyPersonInfoType.base
    var placeholder = ""
    var detailText = Variable<String>("")
    private var save_detailText = ""
    
    
    // type为 .choose 的情况系的属性
    var chooseArray = [ApplyChooseModel]() // 供用户选择的数据源
    var chooseType = "" // 请求数据的字段
    var chooseModel = ApplyChooseModel() // 用户选择的数据
    
    
    
    
    init(titleName: String, type: ApplyPersonInfoType, placeholder: String, detailText: String, chooseType: String = "") {
        self.titleName = titleName
        self.type = type
        self.placeholder = placeholder
        self.detailText.value = detailText
        self.chooseType = chooseType
        getData(chooseType)
    }
    
    func save(_ key: String) {
        save_detailText = detailText.value
        let jsonString = self.toJSONString()
        UserDefaults.standard.set(jsonString, forKey: CDUserDefaultKey.applySaveInfoKey+key)
    }
    
    func clean(_ key: String) {
        UserDefaults.standard.set(nil, forKey: CDUserDefaultKey.applySaveInfoKey+key)
    }
    
    func config(_ key: String) {
        let jsonString = UserDefaults.standard.string(forKey: CDUserDefaultKey.applySaveInfoKey+key)
        
        if let m = ApplyPersonInfo(JSONString: jsonString ?? "") {
            titleName = m.titleName
            type = m.type
            placeholder = m.placeholder
            save_detailText = m.save_detailText
            detailText.value = m.save_detailText
            chooseArray = m.chooseArray
            chooseType = m.chooseType
            chooseModel = m.chooseModel
        }
    }
    
    func getData(_ type: String, finished: (()->(Void))? = nil) {
        if type.count == 0 {
            return
        }
        if let dataString = UserDefaults.standard.object(forKey: CDUserDefaultKey.applyChooseInfoKey + type) as? String
        {
            if let array = Mapper<ApplyChooseModel>().mapArray(JSONString: dataString), !array.isEmpty {
                self.chooseArray = array
                return
            }
        }
        
        let json = ["DictNo": type].convertToJsonString()
        let paramString = "<json>"+json+"</json>"
        
        NetworkingHandle.fetchNetworkData(method: "getDictInfo", paramString: paramString, success: {[weak self] (dict) in
            if let dataString = dict["DictDetail"] as? String {
                if let array = Mapper<ApplyChooseModel>().mapArray(JSONString: dataString) {
                    self?.chooseArray = array
                    let jsonString = array.toJSONString()
                    UserDefaults.standard.set(jsonString, forKey: CDUserDefaultKey.applyChooseInfoKey+type)
                    if finished != nil {
                        finished!()
                    }
                }
            }
        }) {
            print("")
        }
    }
    
}

class ApplyChooseModel: Mappable, PickerProtocol {
    
    var id:Int = 0
    var name = ""
    var showName: String {
        return name
    }
    
    init() {
    }
    
    required init?(map: Map) {
    }
    
    func mapping(map: Map) {
        id <- map["DETAILNO"]
        name <- map["DETAILNAME"]
        if name.count == 0 {
            id <- map["DetailNo"]
            name <- map["DetailName"]
        }
    }
}

protocol PickerProtocol {
    var showName: String { get }
}
